from flask import Flask, request, render_template
from flask_cors import CORS
import uuid
from Auth import Task,outerFunction
import json
from flask_apscheduler import APScheduler

app = Flask(__name__)
CORS(app, resources=r'/*')
g_judge = None
g_bRegister = False


@app.route('/getfunction', methods=['get'])
def my_test():
    task_id = str(uuid.uuid1())
    result_id = str(uuid.uuid1())
    tmp = Task('a', task_id, result_id)
    mac=tmp.functionOne()
    return mac


@app.route('/postfunction', methods=['post'])
def object_info():
    jsonparams = request.get_json(force=True)
    task_id = jsonparams["task_id"]
    result_id = jsonparams["result_id"]
    tep=Task('a',task_id,result_id)
    res=tep.functionTwo()
    if not isinstance(task_id, int):
        err = {"error": "step must be a integer!"}
        return json.dumps(err)
    return json.dumps(res)



@app.route('/getandpostfunction', methods=['get', 'post'])
def index():
    if request.method == 'GET':
        return render_template('hello.html')
    elif request.method == 'POST':
        jsonparams = request.get_json(force=True)
        task_id = jsonparams["task_id"]
        result_id = jsonparams["result_id"]
        tmp = Task('a', task_id, result_id)
        mac = tmp.functionOne()
        outerRes=outerFunction()
        zone={}
        zone[mac]=outerRes
        return zone


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()
    app.run(host='0.0.0.0', port=4000, debug=False, threaded=True)
    print(" main stop -----------------")


# See PyCharm help at https://www.jetbrains.com/help/pycharm/
